package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.constant.MessageConstant;
import com.itcast.constant.RedisConstant;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.entity.Result;
import com.itcast.pojo.Setmeal;
import com.itcast.service.SetmealService;
import com.itcast.untils.QiniuUtils;
import com.itcast.untils.Untilali;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;


@RestController
@RequestMapping("/setmeal")
public class SetmealController {
   @RequestMapping("/upload")
    //把图片上传到七牛云
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
    //获取文件原始名
        String originalFilename = imgFile.getOriginalFilename();
        int indexOf = originalFilename.lastIndexOf(".");
        //获取文件后缀名
        String substring = originalFilename.substring(indexOf);
        //使用uuid随机生成文件的名称
        String nameFile = UUID.randomUUID().toString() + substring;
        //上传文件
        //QiniuUtils.upload2Qiniu(imgFile.getContentType(),nameFile);
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),nameFile);
        } catch (IOException e) {
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
        //将文件存储到jedis中
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,nameFile);
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,nameFile);
    }
    //@RequestMapping("/upload")
    //把图片上传到aliyun
    public Result upload1(@RequestParam("imgFile") MultipartFile imFile) throws IOException {
        //获取文件的名称
        String originalFilename = imFile.getOriginalFilename();
        int indexOf = originalFilename.lastIndexOf('.');
        //获取文件的后缀名
        String substring = originalFilename.substring(indexOf);
        //利用uuid随机文件名
        String filename = UUID.randomUUID().toString() + substring;
        //通过字节数组上传文件
        Untilali.uploadByte(imFile.getBytes(),filename);
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,filename);
    }
    @Reference
    private SetmealService stemealService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 查询所有数据
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult;
        try {
            pageResult = stemealService.findAll(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
    }

    /**
     * 添加数据
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            stemealService.add(setmeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 删除套餐
     * @param setmeal
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Setmeal setmeal){
        try {
            stemealService.delete(setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除套餐失败");
        }
        return new Result(true,"删除套餐成功");
    }
}
